<?php

function insertUser($login, $pass, $role = 'admin user none') {
    if (isAdmin()) {
        $query = sprintf(SQL::$INSERT_USER, mysql_real_escape_string($login), mysql_real_escape_string(hashByMatu($login, $pass)), mysql_real_escape_string($role));
        mq($query, getCon());
    }
}

function deleteDynCisMap($fkname) {
    $query = sprintf(SQL::$DELETE_dyncis, mysql_real_escape_string($fkname));
    mq($query, getCon());
}

function deleteUser($login) {
    if (isAdmin()) {
        $query = sprintf(SQL::$DELETE_USER, mysql_real_escape_string($login));
        mq($query, getCon());
    }
}

function deleteSometing($name, $table = "img") {
    if (isAdmin()) {
        $query = sprintf(SQL::$DELETE_SOMETING, mysql_real_escape_string($table), mysql_real_escape_string($name));
        mq($query, getCon());
    }
}

function deleteItem($itemid) {
    $query = sprintf(SQL::$DELETE_ITEM_BYITEMID, mysql_real_escape_string($itemid));
    mq($query, getCon());
}

function selectItemid($value_name) {
    $query = sprintf(SQL::$SELECT_ITEMMAP_ITEMID, mysql_real_escape_string($value_name));
    $itemid = fetch(mq($query, getCon()));
    return $itemid["itemid"];
}

function insertNumber($name, $value = 0) {
    $query = sprintf(SQL::$INSERT_NUMBER, mysql_real_escape_string($name), mysql_real_escape_string($value), getUserId());
    return mq($query, getCon());
}

function addCat($catName) {
    $query = sprintf(SQL::$SELECT_ITEMMAP_ITEMIDS, mysql_real_escape_string($catName));
    $result = mq($query, getCon());
    $row = mysql_fetch_assoc($result);
    if ($row) {
        $itemid = $row["itemid"];
        $query = sprintf(SQL::$SELECT_ITEMMAP_CATPARTS, mysql_real_escape_string($catName), $itemid);
        $result = mq($query, getCon());
        $newItemId = genNum();
        while ($row = fetch($result)) {
            $rowTemp = fetch(selectValueFromAll($row["value_name"]));
            $valueNameTemp = insertItemMap($row["item_name"], $catName, $newItemId);
            switch ($rowTemp["tableName"]) {
                case "img":

                    break;
                case "dynstruc":
                    insertDynSruc($valueNameTemp);
                    break;
                case "date":
                    insertDate($valueNameTemp);
                    break;
                case "text_" . getLangName():
                    createText($valueNameTemp);
                    break;
                case "number":
                    insertNumber($valueNameTemp);
                    break;
                default :
                    createLog("addCat: row not found");
                    break;
            }
        }
    }
}

function createText($name = null, $data = null) {
    if ($name == null)
        $name = genName();
    $langs = $GLOBALS['langList'];
    foreach ($langs as $value) {
        $query = str_replace("text_ ", "text_" . $value::getClass() . " ", SQL::$INSERT_TEXT);
        $query = sprintf($query, mysql_real_escape_string($name), mysql_real_escape_string($data == null || $data == "" ? Settings::$defultText : $date), getUserId());
        mq($query, getCon());
    }
}

/**
 *
 * @param type $idcismap
 * @return array{"fkname"} 
 */
function selectDyncismap($idcismap) {
    $query = sprintf(SQL::$SELECT_DYNCISMAP, mysql_real_escape_string($idcismap));
    return mq($query, getCon());
}

function constructDyn($dynsrucid, $name = null) {
    if ($name == null)
        $name = genName();
    insertDynSruc($dynsrucid, $name);
}

function insertDynCisMapSimple($dynsrucid, $fkname) {
    $query = sprintf(SQL::$INSERT_DYNCISMAP_SIMPLE, mysql_real_escape_string($dynsrucid), mysql_real_escape_string($fkname));
    mq($query, getCon());
}

function insertDynSruc($name = null, $dynsrucid = null) {
    if ($name == null)
        $name = genName();
    if ($dynsrucid == null) {
        $dynsrucid = genNum();
    }
    $query = sprintf(SQL::$INSERT_DYN, mysql_real_escape_string($name), getUserId(), mysql_real_escape_string($dynsrucid));
    mq($query, getCon());
}

function insertDate($name = null, $data = null) {
    if ($name == null)
        $name = genName();
    $query = sprintf(SQL::$INSERT_DATE, mysql_real_escape_string($name), mysql_real_escape_string($data == null ? "NULL" : $data), getUserId());
    mq($query, getCon());
}

function updateData($table, $name, $data) {
    $query = sprintf(SQL::$UPDATE_DATA, mysql_real_escape_string($table), mysql_real_escape_string($data), getUserId(), mysql_real_escape_string($name));
    return mq($query, getCon());
}

/**
 * fullSize,mediumSize,smallSize
 * 
 * @param type $name
 * @param type $imageSize
 * @return textData,numberData,imgData,dateData
 */
function selectValueFromAll($name, $imageSize = "fullSize") {
    $query = sprintf(SQL::$SELECT_ALL_VALUES, mysql_real_escape_string($name), mysql_real_escape_string($imageSize));
    return mq($query, getCon());
}

function selectUser($login, $pass) {
    $query = sprintf(SQL::$SELECT_USER, mysql_real_escape_string($login), mysql_real_escape_string(hashByMatu($login, $pass))
    );
    return mq($query, getCon());
}

function selectText($name) {
    $query = sprintf(SQL::$SELECT_TEXT, mysql_real_escape_string($name));
    return mq($query, getCon());
}

function selectItems($itemType_name) {
    $ret =  Dynstruc::DBselectItems($itemType_name);
    if ($ret != null) return $ret;
    $query = sprintf(SQL::$SELECT_ITEMSID, mysql_real_escape_string($itemType_name));
    return mq($query, getCon());
}

function selectItem($itemId) {
    $query = sprintf(SQL::$SELECT_ITEM_ID, mysql_real_escape_string($itemId));
    return mq($query, getCon());
}

function selectItemValue($itemId, $itemName) {
    $query = sprintf(SQL::$SELECT_ITEM_ID_NAME, mysql_real_escape_string($itemId), mysql_real_escape_string($itemName));
    return mq($query, getCon());
}

function selectType($name) {
    $query = sprintf(SQL::$SELECT_ITEMTYPE_NAME, mysql_real_escape_string($name));
    return mq($query, getCon());
}

function genName() {
    return "gen" . genNum();
}

function genNum() {
    $query = sprintf(SQL::$INSERT_GENID);
    mq($query, getCon());
    return mysql_insert_id();
}

function createManifest() {
    $query = sprintf(SQL::$CREATE_MANIFEST);
    mq($query, getCon());
}

function dropManifest() {
    if (isAdmin()) {
        $query = sprintf(SQL::$DROP_MANIFEST);
        mq($query, getCon());
    }
}

function selectManifest() {
    $query = sprintf(SQL::$SELECT_MANIFEST);
    $result = mq($query, getCon());
    return $result;
}

function insertManifest($templateName, $templatePath, $role) {
    $query = sprintf(SQL::$INSERT_MANIFEST_ITEM, mysql_real_escape_string($templateName), mysql_real_escape_string($templatePath), mysql_real_escape_string($role));
    mq($query, getCon());
    return mysql_insert_id();
}

function insertItemType($typeName) {
    $textName = genName();
    $query = sprintf(SQL::$INSERT_ITEMTYPE, mysql_real_escape_string($typeName), $textName);
    mq($query, getCon());
    return $textName;
}

function insertItemMap($itemName, $itemType_name, $itemId, $value_name = null) {
    if ($value_name == null) {
        $value_name = genName();
    }
    $query = sprintf(SQL::$INSERT_ITEMMAP, mysql_real_escape_string($itemName), mysql_real_escape_string($itemType_name), mysql_real_escape_string($value_name), mysql_real_escape_string($itemId));
    mq($query, getCon());
    return $value_name;
}

?>
